/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
  // 正常层序遍历，遍历其节点children
    vector<vector<int>> levelOrder(Node* root) {
        vector<vector<int>> res;
        queue<Node*> q;
        if(nullptr != root) q.push(root);
        while(!q.empty()){
            size_t size = q.size();
            vector<int> level;
            level.reserve(size);   //提前分配好容量，提高效率
            for(int i = 0 ;i < size; ++i){
                Node* node = q.front();
                q.pop();
                level.push_back(node->val);
                // 将所有子节点放入队列
                for(int j = 0; j < node->children.size(); ++j){
                     if(node->children[j] != nullptr)
                        q.push(node->children[j]);
                }      
            }
            res.push_back(level);
        }
        return res;
    }
};